package com.hoperun.checkcloth.ui

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hoperun.checkcloth.R
import com.hoperun.checkcloth.adapter.StuffCheckInfoAdapter
import com.hoperun.checkcloth.databinding.StuffCheckInfoActivityBinding
import com.hoperun.checkcloth.entity.MaterialEntity
import com.hoperun.checkcloth.ui.dialog.SimpleDialog
import com.hoperun.checkcloth.viewmodel.StuffCheckInfoViewModel

class StuffCheckInfoActivity : AppCompatActivity() {
    private lateinit var binding: StuffCheckInfoActivityBinding
    private lateinit var viewModel: StuffCheckInfoViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProvider(this)[StuffCheckInfoViewModel::class.java]
        binding = DataBindingUtil.setContentView(this, R.layout.stuff_check_info_activity)
        setupBinding()
        setupViewModel()
    }


    private fun setupBinding() {
        binding.vm = viewModel

        val list = intent.getSerializableExtra("materialList") as List<MaterialEntity>
        val adapter = StuffCheckInfoAdapter().apply {
            setDataSet(list)
            itemSelected = { item ->
                viewModel.onItemSelected(item)
            }
        }
        binding.rlStuffList.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            this.adapter = adapter
        }
    }

    private fun setupViewModel() {
        viewModel.getSelectItem.observe(this, { item ->
            Intent().apply {
                putExtra("material", item)
                setResult(RESULT_OK, this)
            }
            finish()
        })

        viewModel.dialogMessage.observe(this, { message ->
            SimpleDialog.newInstance(message).show(supportFragmentManager, "SimpleDialog")
        })
    }


}